package com.ja.openchat.views;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.listener.OnRedPacketCallback;
import com.ja.openchat.ui.chat.redpacket.RedPacketDetailActivity;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.manager.RedPacketManager;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.redpacket.RedPacketInfo;
import com.zy.openchat.core.model.redpacket.RedPacketPickUser;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.RedPacketAnim;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseActivity;

public class GetRedPacketDialog extends CenterPopupView {

    private RedPacketInfo mRedPacketInfo;
    private OnRedPacketCallback mCallback;

    public GetRedPacketDialog(@NonNull Context context, RedPacketInfo redPacketInfo, OnRedPacketCallback callback) {
        super(context);
        mRedPacketInfo = redPacketInfo;
        mCallback = callback;
    }

    private TextView red_packet_title,red_packet_content,look_detail_btn;
    private ImageView open_red_btn;

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_get_red_packet;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        open_red_btn = findViewById(R.id.open_red_btn);
        red_packet_title = findViewById(R.id.red_packet_title);
        red_packet_content = findViewById(R.id.red_packet_content);
        look_detail_btn = findViewById(R.id.look_detail_btn);
        findViewById(R.id.close_btn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if(mRedPacketInfo == null){
            return;
        }
        look_detail_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RedPacketDetailActivity.startActivity(getContext(),mRedPacketInfo);
            }
        });
        red_packet_title.setText(String.format("%s发出的现金红包", UserInfo.getUserInfo(mRedPacketInfo.from).displayName()));
        TelegramManager.RpState rpState = mRedPacketInfo.getRpState();
        switch (rpState){
            case RpState_Expire:
                red_packet_content.setText("已过期\n红包已超过24小时");
                look_detail_btn.setVisibility(View.GONE);
                open_red_btn.setVisibility(View.GONE);
                RedPacketManager.getRedPacketManager().appendId(mRedPacketInfo.redPacketId);
                if(mCallback!=null){
                    mCallback.onResult(mRedPacketInfo);
                }
                break;
            case RpState_Done:
                red_packet_content.setText("红包已领完\n很抱歉, 你来晚了");
                look_detail_btn.setVisibility(View.VISIBLE);
                open_red_btn.setVisibility(View.GONE);
                RedPacketManager.getRedPacketManager().appendId(mRedPacketInfo.redPacketId);
                if(mCallback!=null){
                    mCallback.onResult(mRedPacketInfo);
                }
                break;
            case RpState_To_Get:
                red_packet_content.setText(mRedPacketInfo.title);
                look_detail_btn.setVisibility(mRedPacketInfo.from == UserInfo.getUserInfo().id?View.VISIBLE:View.GONE);
                open_red_btn.setVisibility(View.VISIBLE);
                open_red_btn.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RedPacketManager.getRedPacketManager().appendId(mRedPacketInfo.redPacketId);
                        RedPacketAnim packetAnim = new RedPacketAnim();
                        packetAnim.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                openRed();
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        open_red_btn.startAnimation(packetAnim);

                    }
                });
                break;
        }

    }

    private void openRed(){
        if(getContext() instanceof BaseActivity){
            ((BaseActivity)getContext()).showProgress();
        }
        TelegramManager.getTelegramManager().gotRp(getContext(),mRedPacketInfo.redPacketId, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                if(obj!=null&&!obj.ok){
                    ToastUtils.showShort(obj.errString);
                }
                if(getContext() instanceof BaseActivity){
                    ((BaseActivity)getContext()).hideProgress();
                }
                if(mRedPacketInfo!=null&&obj.ok){
                    if(obj!=null&&obj.ok&&obj.data!=null&&obj.data instanceof List){
                        mRedPacketInfo.users = (List<RedPacketPickUser>)obj.data;
                    }
                    if(mCallback!=null){
                        mCallback.onResult(mRedPacketInfo);
                    }
                    dismissWith(new Runnable() {
                        @Override
                        public void run() {
                            RedPacketDetailActivity.startActivity(getContext(),mRedPacketInfo);
                        }
                    });
                }else{
                    dismiss();
                }


            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                if(getContext() instanceof BaseActivity){
                    ToastUtils.showShort("请求超时，请稍后重试");
                }
                dismiss();
            }
        });
    }

    @Override
    protected int getPopupWidth() {
        return (int) (XPopupUtils.getAppWidth(getContext()) * .72f);
    }
}
